<?php
namespace Admin\Model;
use Think\Model;
class AdminUserModel extends Model{
    protected $tableName = "admin_user";
    protected $tablePrefix = "dangjian_";
    protected $showRow = 10;

    //后台管理员登录
    public function login ($data)
    {
        $name = $data['name'];
        $password = $data['password'];
        $res = $this->where("name='$name' and password='$password'")->getField('id');
        if(!empty($res)){
            $_SESSION['admin']['name'] = $name;
            $_SESSION['admin']['id'] = $res['id'];
            return 1;
        }
        return 0;
    }
    //添加管理员到数据库
    public function addUser ($data)
    {
        $data['create_time'] = date("Y-m-d H:i:s");
        $res = $this->data($data)->add();
        if($res > 0){
            return 1;
        }
        return 0;
    }
    //根据姓名判断管理员是否已存在
    public function queryUser ($name)
    {
        $res = $this->where("name='$name'")->getField('id');
        if(!empty($res)){
            return 1;//该管理员已存在
        }
        return 0;
    }
    //查询所有管理员
    public function queryAllAdminUser($data)
    {
        $name = $data['name'];
        $map = array();
        if(!empty($name)){
            $map['au.name'] = array("LIKE", '%' . $name . '%');
        }
        //查询总记录数
        $count = $this->alias("au")
            ->join("dangjian_admin_user_sort aus on aus.id=au.sort_id")
            ->where($map)
            ->count();
        //使用分页
        $page = new \Think\Page($count,$this->showRow);
        $show = $page->show();
        $list = $this->alias("au")
            ->join("dangjian_admin_user_sort aus on aus.id=au.sort_id")
            ->Field("au.id,au.name,au.create_time,au.update_time,aus.name sort_name")
            ->where($map)
            ->limit($page->firstRow.','.$page->listRows)
            ->select();
        $res['show'] = $show;
        $res['list'] = $list;
        return $res;
    }

    public function deleteAdminUser ($id)
    {
        $this->where("id=$id")->delete();
    }
}